package weaver.interfaces.cese.duss.action.crm;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-23
 * @Description: 客户信息录入生成客户编码
 * @ModifyLog:
 **/
/*
 *@Description:// 客户在登记（formtable_main_71）的时候，第二个节点，在uf_SRM_gyskpx 供应商卡片（gysbm），
// uf_CRM_Competitor 竞争对手库（competitor_code），uf_CRM_Fangku 相关方库（coding），
// 表中根据统一社会信用代码校验一遍，如果查询到了数据就直接引用对应的编码，填写到客户信息录入流程中
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2020/10/23
 */
public class WeaGenerateCustomCodeAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        writeLog( "----WeaGenerateCustomCodeAction-------" + requestid );
        try {
            String sql = "select customer_code,customer_name,shtyxydm,customer_identity_mark from " + tablename + " where requestid=?";
            rs.executeQuery(sql , requestid );
            rs.writeLog(sql,requestid);
            rs.next();
            String customer_code = Util.null2String( rs.getString( "customer_code" ) );//客户编码
            String customer_name = Util.null2String( rs.getString( "customer_name" ) );//客户编码
            if ("".equals( customer_code )) {
                String code = "";
                String shtyxydm = Util.null2String( rs.getString( "shtyxydm" ) );//统一社会编码
                String customer_type = Util.null2String( rs.getString( "customer_identity_mark" ) );//客户类型
                rs.executeQuery("select code from view_dwxx where shtyxydm=?",shtyxydm);
                rs.writeLog( "select code from view_dwxx where shtyxydm=?",shtyxydm );
                if(rs.next()){
                    code = Util.null2String(rs.getString( "code" ));
                }
                if ("".equals(code)) {
                    rs.executeQuery("select code from view_dwxx where name=?",customer_name);
                    rs.writeLog( "select code from view_dwxx where name=?",customer_name );
                    if(rs.next()){
                        code = Util.null2String(rs.getString( "code" ));
                    }
                }

                if ("".equals(code)) {
                    code = getFlowWaterNum( customer_type );
                }
                rs.executeUpdate( "update " + tablename + " set customer_code='" + code + "' where requestid=?", requestid );
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "生成客户编码接口异常" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
    public static String getFlowWaterNum(String khlx) {
        RecordSet rs = new RecordSet();
        String sql = "select max(lsh) from uf_CRM_KHLSH where xxly='0' and khlx  = ?";
        rs.executeQuery( sql, khlx );
        rs.writeLog( sql, khlx );
        rs.next();
        String lsh = Util.null2String( rs.getString( 1 ) );
        String customer_code = lsh;
        String newLsh = autoGenericCode( lsh, lsh.length() );
        String sql1 = "update uf_CRM_KHLSH set lsh='" + newLsh + "' where  xxly='0' and khlx  = ?";
        rs.executeUpdate( sql1, khlx );
        rs.writeLog( sql1, khlx );
        return customer_code;
    }

    //不够位数的在前面补0，保留num的长度位数字
    public static String autoGenericCode(String code, int num) {
        String result = "";
        result = String.format( "%0" + num + "d", Integer.parseInt( code ) + 1 );
        return result;
    }

}
